<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.service.mapper;

<%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
import <%= packageName %>.domain.<%= asEntity('User') %>;
import <%= packageName %>.service.dto.<%= asDto('User') %>;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Mapper for the entity {@link <%= asEntity('User') %>} and its DTO called {@link <%= asDto('User') %>}.
 *
 * Normal mappers are generated using MapStruct, this one is hand-coded as MapStruct
 * support is still in beta, and requires a manual step with an IDE.
 */
@Service
public class UserMapper {

    public List<<%= asDto('User') %>> usersToUserDTOs(List<<%= asEntity('User') %>> users) {
        return users.stream()
            .filter(Objects::nonNull)
            .map(this::userToUserDTO)
            .collect(Collectors.toList());
    }

    public <%= asDto('User') %> userToUserDTO(<%= asEntity('User') %> user) {
        return new <%= asDto('User') %>(user);
    }

    public List<<%= asEntity('User') %>> userDTOsToUsers(List<<%= asDto('User') %>> userDTOs) {
        return userDTOs.stream()
            .filter(Objects::nonNull)
            .map(this::userDTOToUser)
            .collect(Collectors.toList());
    }

    public <%= asEntity('User') %> userDTOToUser(<%= asDto('User') %> userDTO) {
        if (userDTO == null) {
            return null;
        } else {
            <%= asEntity('User') %> user = new <%= asEntity('User') %>();
            user.setId(userDTO.getId());
            user.setLogin(userDTO.getLogin());
            user.setFirstName(userDTO.getFirstName());
            user.setLastName(userDTO.getLastName());
            user.setEmail(userDTO.getEmail());
            <%_ if (databaseType !== 'cassandra') { _%>
            user.setImageUrl(userDTO.getImageUrl());
            <%_ } _%>
            user.setActivated(userDTO.isActivated());
            user.setLangKey(userDTO.getLangKey());
            <%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>
            Set<Authority> authorities = this.authoritiesFromStrings(userDTO.getAuthorities());
            user.setAuthorities(authorities);
            <%_ } _%>
            <%_ if (databaseType === 'cassandra' || databaseType === 'couchbase') { _%>
            Set<String> authorities = this.cleanNullStringAuthorities(userDTO.getAuthorities());
            user.setAuthorities(authorities);
            <%_ } _%>
            return user;
        }
    }

    <%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>

    private Set<Authority> authoritiesFromStrings(Set<String> authoritiesAsString) {
        Set<Authority> authorities = new HashSet<>();

        if (authoritiesAsString != null) {
            authorities = authoritiesAsString.stream().map(string -> {
                Authority auth = new Authority();
                auth.setName(string);
                return auth;
            }).collect(Collectors.toSet());
        }

        return authorities;
    }
    <%_ } _%>
    <%_ if (databaseType === 'cassandra' || databaseType === 'couchbase') { _%>

    private Set<String> cleanNullStringAuthorities(Set<String> authoritiesAsString) {
        Set<String> authorities = new HashSet<>();

        if (authoritiesAsString != null) {
            authorities = authoritiesAsString.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        }

        return authorities;
    }
    <%_ } _%>

    public <%= asEntity('User') %> userFromId(<% if (databaseType === 'mongodb' || databaseType === 'neo4j' || databaseType === 'couchbase' || databaseType === 'cassandra' || authenticationType === 'oauth2') { %>String<% } else { %>Long<% } %> id) {
        if (id == null) {
            return null;
        }
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        user.setId(id);
        return user;
    }
}
